<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

class AdminuserController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'admin'],
        'show' => ['get', 'admin/%d'],
        'store' => ['post', 'admin'],
        'role' =>['get','role'],
        'permission' =>['get','permission'],
        'update' => ['put', 'admin/%d'],
        'destroy' => ['delete', 'admin/%d'],
    ];

    public function index(Request $request)
    {
        $params = [
            'page' => $request->input('page',1),
            'orderBy'=>'created_at',
            'sortedBy'=>'desc',
        ];
        $res = http_admin($this->api_url['index'])->send($params);

        $data = paginate($request, $res['data']);
        return view('Admin.adminuser.index',$data);
    }
    
    public function edit($id){

        $res = http_admin($this->api_url['show'], [$id])->send();

        // 获取所有角色权限 // $res 已通过表关联获取已存储的权限
        $role= http_admin($this->api_url['role'])->send();

        // 获取所有操作权限
        // $permission=http_admin($this->api_url['permission'])->send(['type'=>'all']);
        // $res['data']['permission'] = $permission['data'];

        $res['data']['rolesAll'] = $role['data']['list'];
//        dd($res);
        return view('Admin.adminuser.edit',$res['data']);
    }


    public function create(){
        // 获取所有的 角色权限        
        $role= http_admin($this->api_url['role'])->send();
        $data['role'] = $role['data']['list'];

        // 获取所有的 操作权限        
        // $permission=http_admin($this->api_url['permission'])->send(['type'=>'all']);
        // $data['permission'] = $permission['data'];
        return view('Admin.adminuser.create',$data);
    }

    public function store(Request $request)
    {
        $data = $request->except('_token','_method');
        $res = http_admin($this->api_url['store'])->send($data);
        return $res;
    }

    public function update($id,Request $request)
    {
        $data = $request->except('_token','_method');
//        dd($data);
        $res = http_admin($this->api_url['update'], [$id])->send($data);
        return $res;
//        return redirect('/foradmin/adminuser');
        
    }

    public function destroy($id)
    {
        $res = http_admin($this->api_url['destroy'], [$id])->send();
        return redirect('/foradmin/adminuser');
    }
}